package cn.ekuma.data.ui.android.editor;

import java.util.Enumeration;
import java.util.Vector;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;

import com.openbravo.data.user.DirtyListener;

public class DirtyManager implements View.OnKeyListener,View.OnClickListener, TextWatcher, AdapterView.OnItemSelectedListener {
	
	 private boolean m_bDirty;    
	 protected Vector listeners = new Vector();
	    
	    /** Creates a new instance of DirtyManager */
	    public DirtyManager() {
	        m_bDirty = false;
	    }
	    
	    public void addDirtyListener(DirtyListener l) {
	        listeners.add(l);
	    }
	    
	    public void removeDirtyListener(DirtyListener l) {
	        listeners.remove(l);
	    }
	    
	    protected void fireChangedDirty() { 
	        Enumeration e = listeners.elements();
	        while (e.hasMoreElements()) {
	            DirtyListener l = (DirtyListener) e.nextElement();
	            l.changedDirty(m_bDirty);
	        }
	    }
	    
	    public void setDirty(boolean bValue) {
	        if (m_bDirty != bValue) {
	            m_bDirty = bValue;
	            fireChangedDirty();
	        }
	    }
	    
	    public boolean isDirty() {
	        return m_bDirty;
	    }

		public void onClick(View v) {
			 if (m_bDirty != true) {
		            m_bDirty = true;
		            fireChangedDirty();
		     }
			
		}

		public boolean onKey(View v, int keyCode, KeyEvent event) {
			 if (m_bDirty != true) {
		            m_bDirty = true;
		            fireChangedDirty();
		    }
			return false;
		}

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (m_bDirty != true) {
            m_bDirty = true;
            fireChangedDirty();
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (m_bDirty != true) {
            m_bDirty = true;
            fireChangedDirty();
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
